package com.bwie.student.controller;

import com.alibaba.fastjson.JSONObject;
import com.bwie.common.domain.Friend;
import com.bwie.common.domain.request.FriendRequest;
import com.bwie.common.result.PageResult;
import com.bwie.common.result.Result;
import com.bwie.student.service.FriendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@RestController
@Slf4j
public class FriendController {
    @Autowired
    private FriendService friendService;

    @Autowired
    private HttpServletRequest request;

    @PostMapping("/showFriend")
    public Result<PageResult<Friend>> showFriend(@RequestBody FriendRequest friendRequest){
        log.info("功能名称:列表展示,请求URI:{},请求方式:{},请求参数:{}",
                request.getRequestURI(),
                request.getMethod(),
                JSONObject.toJSONString(friendRequest));
        Result<PageResult<Friend>> pageResultResult = friendService.showFriend(friendRequest);
        log.info("功能名称:列表展示,请求URI:{},请求方式:{},响应结果:{}",
                request.getRequestURI(),
                request.getMethod(),
                JSONObject.toJSONString(pageResultResult));
        return pageResultResult;
    }


    @PostMapping("/fastDfsUpload")
    public Result fastDfsUpload(@RequestParam("file")MultipartFile multipartFile){
        log.info("功能名称:图片上传,请求URI:{},请求方式:{},请求参数:{}",
                request.getRequestURI(),
                request.getMethod(),
                JSONObject.toJSONString(multipartFile));
        Result result = friendService.fastDfsUpload(multipartFile);
        log.info("功能名称:图片上传,请求URI:{},请求方式:{},响应结果:{}",
                request.getRequestURI(),
                request.getMethod(),
                JSONObject.toJSONString(result));
        return result;
    }

    @PostMapping("/insertFriend")
    public Result insertFriend(@RequestBody FriendRequest friendRequest){
        log.info("功能名称:添加,请求URI:{},请求方式:{},请求参数:{}",
                request.getRequestURI(),
                request.getMethod(),
                JSONObject.toJSONString(friendRequest));
        Result result = friendService.insertFriend(friendRequest);
        log.info("功能名称:列表展示,请求URI:{},请求方式:{},响应结果:{}",
                request.getRequestURI(),
                request.getMethod(),
                JSONObject.toJSONString(result));
        return result;
    }
    @PostMapping("/findById")
    public Result findById(@RequestParam String friendId){
        log.info("功能名称:根据Id查询,请求URI:{},请求方式:{},请求参数:{}",
                request.getRequestURI(),
                request.getMethod(),
                friendId);
        Result byId = friendService.findById(friendId);
        log.info("功能名称:根据Id查询,请求URI:{},请求方式:{},响应结果:{}",
                request.getRequestURI(),
                request.getMethod(),
                JSONObject.toJSONString(byId));
        return byId;
    }

    @PostMapping("/updateFriend")
    public Result updateFriend(@RequestBody Friend friend){
        log.info("功能名称:修改,请求URI:{},请求方式:{},请求参数:{}",
                request.getRequestURI(),
                request.getMethod(),
                friend);
        Result result = friendService.updateFriend(friend);
        log.info("功能名称:修改,请求URI:{},请求方式:{},响应结果:{}",
                request.getRequestURI(),
                request.getMethod(),
                result);
        return Result.success(result);
    }


}
